package com.ajie.bluetoothcard.ui;


import java.util.ArrayList;

import com.ajie.bluetoothcard.adapter.CardAdapter;
import com.ajie.bluetoothcard.adapter.DelCardAdapter;
import com.ajie.bluetoothcard.db.DBApi;
import com.ajie.bluetoothcard.domain.Card;
import com.ajie.bluetoothcard.domain.CardType;
import com.ajie.bluetoothcard.pre.Preference;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
/**
 * 批量删除
 * @author Jack
 *
 */
public class DelCardListActivity extends BaseActivity  {

	private DelCardAdapter delCardAdapter;
	private ListView listView;
	private ArrayList<Card> cards = new ArrayList<Card>();
	private DBApi dbApi;
	private View button_layout;
	private boolean isDel = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		dbApi = DBApi.getInstance(this);
		cards = (ArrayList<Card>) getIntent().getSerializableExtra("cards");
		isDel = getIntent().getBooleanExtra("isDel", false);
		showTop(R.string.del_card_list, true);
		btn_next.setVisibility(View.VISIBLE);
		top_title_right.setText(R.string.ok);
		initView();
	}
	
	
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		super.initView();
		button_layout =  findViewById(R.id.button_layout);
		button_layout.setVisibility(View.GONE);
		listView = (ListView) findViewById(R.id.list);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		delCardAdapter = new DelCardAdapter(this);
		listView.setAdapter(delCardAdapter);
		delCardAdapter.setData(cards);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_next:
			delCardIds(listView.getCheckItemIds());
			break;

		default:
			break;
		}
	}
	public void delCardIds(long Ids[]){
		if(Ids.length>0){
			for (int i = 0; i < Ids.length; i++) {
				if(isDel){//彻底删除
					dbApi.delCard(cards.get((int) Ids[i]).getId());
				}else{//放到回收站
					Card card = cards.get((int) Ids[i]);
					card.setIsDel("1");
					dbApi.updateRecord(card);
				}
//				dbApi.delCard(cards.get((int) Ids[i]).getId());
			}
			showToast("删除成功");
		}else{
			showToast("没有删除任何一个");
		}
		this.finish();
	}
}
